package com.lili.collection.core.serializer;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.lili.collection.core.error.ConfigSerializeException;
import com.lili.collection.core.support.ConfigFieldDef;
import com.alibaba.fastjson2.filter.ValueFilter;

/**
 * FieldDefPropertyValueFilter
 *
 * @author lili
 * @date 2023/2/28 17:36
 */
public class FieldDefPropertyValueFilter implements ValueFilter {
    @Override
    public Object apply(Object instance, String name, Object value) {
        ConfigFieldDef fieldDef = GlobalConfigDefPropertyUtil.getFieldDef(instance, name);
        if (fieldDef.required() && ObjectUtil.isEmpty(value))
            throw new ConfigSerializeException(StrUtil.format("{}值不能为空", name));
        return value;
    }
}
